package com.dfbz.filter;

import com.dfbz.service.UserService;
import com.dfbz.utils.XBUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class allFilter implements Filter {
    private UserService userService = new UserService();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding("utf-8");
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setContentType("text/html;charset=utf8");
        String requestURI = request.getRequestURI();
        String target = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        if (request.getSession().getAttribute("loginUser") == null ||
                requestURI.startsWith("/bootstrap") ||         //bootstrap 资源路径下的请求全部放行
                requestURI.startsWith("/css") ||
                requestURI.startsWith("/fonts") ||
                requestURI.startsWith("/img") ||
                requestURI.startsWith("/js") ||
                requestURI.startsWith("/upload") ||
                requestURI.startsWith("/vendor") ||


                target.equals("checkCode.jsp") ||       //放行checkCode.jsp页面
                target.equals("forget.jsp") ||
                target.equals("index.jsp") ||
                target.equals("taglib.jsp") ||
                target.equals("register.jsp") ||


                target.equals("login") ||                  // 放行登录请求
                target.equals("logout") ||
                target.equals("forget") ||
                target.equals("register") ||
                target.equals("sendEmail") ||
                target.equals("checkUsername") ||
                target.equals("wx_login") ||
                target.equals("checkEmail")) {

            //放行资源
            filterChain.doFilter(request, response);

            //记住这里要加return,要不然请求放行了代码还是会往下执行
            return;
        } else {

            /**
             * 受限内容
             */
            HttpSession session = request.getSession();
            if (session.getAttribute("loginUser") != null) {

                //虽然是受限内容,但是仍然处于一次会话中,即:用户已经登录过了
                filterChain.doFilter(request, response);

                return;
            }

            // 获取请求头上loginUser的Cookie信息
            Cookie loginCookie = XBUtils.getCookie(request, "loginUser");

            if (loginCookie != null) {
                //说明请求头上的cookie是有携带登录信息的
                //准备读取cookie中的value(userId)

                Long userId = Long.parseLong(loginCookie.getValue());

                session.setAttribute("loginUser", userService.findByid(userId));
                filterChain.doFilter(request, response);

                return;
            }

            //重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }

    @Override
    public void destroy() {

    }
}
